package chapter.android.aweme.ss.com.homework;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * 作业2：一个抖音笔试题：统计页面所有view的个数
 * Tips：ViewGroup有两个API
 * {@link android.view.ViewGroup #getChildAt(int) #getChildCount()}
 * 用一个TextView展示出来
 * @author Sobinbin
 */
public class Exercises2 extends AppCompatActivity {

    private TextView viewForCount;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_relativelayout);
        viewForCount = findViewById(R.id.tv_center);
        // 不算根的layout视图 结果减1
        String text = String.valueOf(getAllChildViewCount(findViewById(R.id.root))-1);
        viewForCount.setText(text);
    }

    public int getAllChildViewCount(View view) {
        //todo 补全你的代码
        if(view==null)  {return 0;}
        int count = 1;
        // dfs深度优先遍历
        if(view instanceof ViewGroup){
            for(int i=0; i<((ViewGroup) view).getChildCount(); i++){
                count += getAllChildViewCount(((ViewGroup) view).getChildAt(i));
            }
        }
        return count;
    }
}
